/*											CMp3Tags
								Written By Dean Thomas - 01/02/2005
				All source code may be used freely and/or modified, if source is used
				in a commercial application then a mention would be nice =)

				Comments/Bugs/Suggestions to Dean.Thomas[AT]Gmail.com
				Hate/Threats/Annoyance to a[at]b.com												*/


	
									//	Mp3Tags.cpp
#include "stdafx.h"

#include "mp3tags.h"

CMp3Tags::CMp3Tags(void)
{
	m_iFileBytes = 0;
}

CMp3Tags::~CMp3Tags(void)
{
}

int CMp3Tags::OpenFile(LPCTSTR lpFileName)
{
	/*if ( !m_fHandle.Open(lpFileName, CFile::modeReadWrite | CFile::typeBinary ) )
	{
		return -1;
	}*/

	if ( !m_fHandle.Open(lpFileName, CFile::modeRead | CFile::typeBinary ) )
	{
		return -1;
	}

	//If we get here, the file's opened. So, we need to parse the data!

	//We want to set the 'cursor' at 128 bytes before the end of the file. 
	m_iFileBytes = (int) m_fHandle.GetLength();

	m_fHandle.Seek( (m_fHandle.GetLength()-128), 0);


	//Now we parse it's last 128 bytes and store it all.

	int iBytesRead = 0;
	char *pBuf[30];
	CString strTmp;

	//Read the first 3 bytes of the header, to see if it's a TAG.
	iBytesRead = m_fHandle.Read(pBuf, 3);
	strTmp.Format("%s", pBuf);
	strTmp.Insert(iBytesRead, '\0');

	if ( strTmp == "TAG" )
	{
		//If we get here, then it's a ID3v1.0/1 Tag.

		//Read the next 30 bytes, which is the TrackName.
		iBytesRead = m_fHandle.Read(pBuf, 30);
		m_strSongTitle.Format("%s", pBuf);
//		m_strSongTitle.Insert(iBytesRead, '@');
		m_strSongTitle.Insert(iBytesRead, '\0');

		//The next 30 are the Artist.
		iBytesRead = m_fHandle.Read(pBuf, 12);
		m_strArtist.Format("%s", pBuf);
//		m_strArtist.Insert(iBytesRead, '@');
		m_strArtist.Insert(iBytesRead+1, '\0');

		//The next 30 are the Album Name.
		iBytesRead = m_fHandle.Read(pBuf, 30);
		m_strAlbum.Format("%s", pBuf);
		m_strAlbum.Insert(iBytesRead, '\0');

		//The next 4 bytes are Year.
		iBytesRead = m_fHandle.Read(pBuf, 4);
		m_strYear.Format("%s", pBuf);
		m_strYear.Insert(iBytesRead, '\0');

		//The next 28 are comments.
		iBytesRead = m_fHandle.Read(pBuf, 28);
		m_strComment.Format("%s", pBuf);
		m_strComment.Insert(iBytesRead, '\0');

		return 0;

	}

	return -1;
	
}

int CMp3Tags::SetSongTitle(LPCTSTR lpSongName)
{
	//Set the cursor on the file to -95 Bytes from the end.
	m_fHandle.SeekToBegin();
	m_fHandle.Seek( ( SONG_TITLE_OFFSET ) , 0 );

	char *pSongTitle = new char[30];
	strcpy(pSongTitle, lpSongName);
	
	//Try to write to the file, if we get a error, report it.
	TRY
	{
		m_fHandle.Write(pSongTitle, 30);
	}
	CATCH(CFileException, e)
	{
		LPTSTR lpError = "";
		e->GetErrorMessage(lpError, 255);
		OutputDebugString(lpError);
		e->Delete();
		return -1;
	}
	END_CATCH

	//delete our memory allocation.
	delete pSongTitle;

	return 0;
}

int CMp3Tags::SetArtist(LPCTSTR lpArtist)
{
	m_fHandle.SeekToBegin();
	m_fHandle.Seek( ( SONG_ARTIST_OFFSET ) , 0 );

	char *pArtist = new char[30];
	strcpy(pArtist, lpArtist);

	TRY
	{
		m_fHandle.Write(pArtist, 30);
	}
	CATCH(CFileException, e)
	{
		LPSTR lpError = "";
		e->GetErrorMessage(lpError, 255);
		OutputDebugString(lpError);
		e->Delete();
		return -1;
	}
	END_CATCH

	delete pArtist;

	return 0;
}

int CMp3Tags::SetAlbum(LPCTSTR lpAlbum)
{
	m_fHandle.SeekToBegin();
	m_fHandle.Seek( ( SONG_ALBUM_OFFSET ) , 0 );

	char *pAlbum = new char[30];
	strcpy(pAlbum, lpAlbum);

	TRY
	{
		m_fHandle.Write(pAlbum, 30);
	}
	CATCH(CFileException, e)
	{
		LPSTR lpError = "";
		e->GetErrorMessage(lpError, 255);
		OutputDebugString(lpError);
		e->Delete();
		return -1;
	}
	END_CATCH
	
	delete pAlbum;

	return 0;

}

int CMp3Tags::SetYear(LPCTSTR lpYear)
{
	m_fHandle.SeekToBegin();
	m_fHandle.Seek( ( SONG_YEAR_OFFSET ), 0 );

	char *pYear = new char[5];
	strcpy(pYear, lpYear);

	TRY
	{
		m_fHandle.Write(pYear, 4);
	}
	CATCH(CFileException, e)
	{
		LPSTR lpError = "";
		e->GetErrorMessage(lpError, 255);
		OutputDebugString(lpError);
		e->Delete();
		return -1;
	}
	END_CATCH

	delete pYear;
	
	return 0;
}

int CMp3Tags::SetComment(LPCTSTR lpComment)
{
	m_fHandle.SeekToBegin();
	m_fHandle.Seek( ( SONG_COMMENT_OFFSET ), 0 );

	char *pComment = new char[28];
	strcpy(pComment, lpComment);

	TRY
	{
		m_fHandle.Write(pComment, 28);
	}
	CATCH( CFileException, e )
	{
		LPSTR lpError = "";
		e->GetErrorMessage(lpError, 255);
		OutputDebugString(lpError);
		e->Delete();
		return -1;
	}
	END_CATCH

	delete pComment;

	return 0;
}